const cloud = require('wx-server-sdk')

cloud.init({
    env: 'j-1g278wytc94978ae'
})

exports.main = async (event, context) => {
    const fun_name = event.fun_name
    const collectionName = event.collection_name
    const params = event.params
    switch (fun_name) {
        case "add_collect_video":
            return add_collect_video(collectionName, params)
        default:
            return {
                errMsg: '云函数没有此方法'
            }
    }
}

async function add_collect_video (collection_name, paramsData) {
    try {
        return cloud.database().collection(collection_name).add({
            data: {
                open_id: paramsData.open_id,
                video_id: paramsData.id,
                url: paramsData.url,
                title: paramsData.title,
                poster: paramsData.poster,
                look_num: paramsData.look_num,
                watch_time: paramsData.watch_time
            }
        })
    } catch (error) {
        cloud.logger().error({
            error
        });
        return error;
    }
}